﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; // 用于JSON序列化和反序列化

public class ApiClient
{
    private static readonly HttpClient _httpClient = new HttpClient();




    // GET请求
    public async Task<string> GetAsync(string url, Dictionary<string, string> parameters = null)
    {
        try
        {
            // 如果有参数，构建查询字符串
            if (parameters != null)
            {
                var queryString = new StringBuilder("?");
                foreach (var param in parameters)
                {
                    queryString.Append($"{Uri.EscapeDataString(param.Key)}={Uri.EscapeDataString(param.Value)}&");
                }
                url += queryString.ToString().TrimEnd('&');
            }

            // 发送GET请求
            HttpResponseMessage response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode(); // 确保请求成功

            // 读取响应内容
            string responseBody = await response.Content.ReadAsStringAsync();
            return responseBody;
        }
        catch (HttpRequestException e)
        {
            Console.WriteLine($"GET请求错误: {e.Message}");
            return null;
        }
    }



    public async Task<string> PostAsync(string url, Dictionary<string, string> parameters = null)
    {
        try
        {

            // 如果有参数，构建查询字符串
            if (parameters != null)
            {
                var queryString = new StringBuilder("?");
                foreach (var param in parameters)
                {
                    queryString.Append($"{Uri.EscapeDataString(param.Key)}={Uri.EscapeDataString(param.Value)}&");
                }
                url += queryString.ToString().TrimEnd('&');
            }

            // 发送GET请求
            HttpResponseMessage response = await _httpClient.PostAsync(url,null);
            response.EnsureSuccessStatusCode(); // 确保请求成功

            // 读取响应内容
            string responseBody = await response.Content.ReadAsStringAsync();
            return responseBody;
        }
        catch (HttpRequestException e)
        {
            Console.WriteLine($"GET请求错误: {e.Message}");
            return null;
        }
    }

    // POST请求（支持JSON数据）
    public async Task<string> PostAsync(string url, object data)
    {
        try
        {

            // 将对象序列化为JSON字符串
            string json = JsonConvert.SerializeObject(data);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            // 发送POST请求
            HttpResponseMessage response = await _httpClient.PostAsync(url, content);
            response.EnsureSuccessStatusCode(); // 确保请求成功

            // 读取响应内容
            string responseBody = await response.Content.ReadAsStringAsync();
            return responseBody;
        }
        catch (HttpRequestException e)
        {
            Console.WriteLine($"POST请求错误: {e.Message}");
            return null;
        }
    }
}